package com.powerunion.datacollection.service;

import java.util.List;

import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.PendingTaskDao;

import com.powerunion.util.log.LogHelper;

public class PendingTaskManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			PendingTaskManager.class);
	private PendingTaskDao dao = null;

	public PendingTaskManager() {
		super();
		this.dao = (PendingTaskDao) DaoFactory.getInstance().getDao(
				PendingTaskDao.class);
	}

	public String findPendingTaskByUser(String userCode) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(userCode)) {
			String[] args = { "userCode" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter userCode NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		List pendingTasks = this.dao.findPendingTaskByUser(userCode);
		JSONArray jsonArr = JSONArray.fromObject(pendingTasks);
		String result = "{results:" + jsonArr + "}";
		return result;
	}
	
	public List getPendingTaskByUser(String userCode) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(userCode)) {
			String[] args = { "userCode" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter userCode NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.findPendingTaskByUser(userCode);
	}
	
	public int findPendingTaskNumByUser(String userCode) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(userCode)) {
			String[] args = { "userCode" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter userCode NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		List pendingTasks = this.dao.findPendingTaskByUser(userCode);
		int num = pendingTasks.size();
		return num;
	}
	
	public String findPendingTask4WebService(String userCode) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(userCode)) {
			String[] args = { "userCode" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter userCode NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		List tasks = this.dao.findPendingTask4WebService(userCode);
		
		JSONArray jsonArr = JSONArray.fromObject(tasks);

		return jsonArr.toString();
	}
	
	public static void main(String[] args) {
		String userCode = "gp";
		PendingTaskManager ptm = new PendingTaskManager();
		int num = 0;
		try{
		 num = ptm.findPendingTaskNumByUser(userCode);
		}catch(Exception e){
			throw new SystemException("数据报送系统获取待办任务出现异常！");
		}
		
		System.out.println("num:"+num);
		
	}

}
